// pages/article/article.js
//var util = require('../../utils/util.js')

var app = getApp()
//格式化正文body部分

function getMp4Info(response) {
    var languagePlaylist = [];
    var streams = response.data.stream;
    streams.forEach(function (ele, idx) {
        var is_mp4 = ele.m3u8_url.indexOf('mp4') > -1;
        var language_zh = ele.audio_lang == 'guoyu';
        var language_en = ele.audio_lang == 'en';
        var language_tw = ele.audio_lang == 'tai';
        var language_df = ele.audio_lang == 'default';

        if (is_mp4 && language_df) {
            var obj = {};
            obj.m3u8_url = ele.m3u8_url;
            obj.list = [];
            obj.lang = 'default';
            obj.milliseconds_video = ele.milliseconds_video;
            ele.segs.forEach(function (vo, idx) {
                obj.list.push(vo);
            });
            languagePlaylist.push(obj);
        }

        if (is_mp4 && language_zh) {
            var obj = {};
            obj.m3u8_url = ele.m3u8_url;
            obj.list = [];
            obj.lang = 'guoyu';
            obj.milliseconds_video = ele.milliseconds_video;
            $(ele.segs).each(function (idx, vo) {
                obj.list.push(vo);
            });
            languagePlaylist.push(obj);
        }
        if (is_mp4 && language_en) {
            var obj = {};
            obj.m3u8_url = ele.m3u8_url;
            obj.list = [];
            obj.lang = 'en';
            obj.milliseconds_video = ele.milliseconds_video;
            $(ele.segs).each(function (idx, vo) {
                obj.list.push(vo);
            });
            languagePlaylist.push(obj);
        }
        if (is_mp4 && language_tw) {
            var obj = {};
            obj.m3u8_url = ele.m3u8_url;
            obj.list = [];
            obj.lang = 'tai';
            obj.milliseconds_video = ele.milliseconds_video;
            $(ele.segs).each(function (idx, vo) {
                obj.list.push(vo);
            });
            languagePlaylist.push(obj);
        }
    });
    return languagePlaylist;

}



Page({

    /**
     * 页面的初始数据
     */
    data: {
        article: {},
        mp4Src: '',
        showButton: false,
        playList: []
    },
    playMp4Part: function (event) {
        //console.log(event);
        var videoSrc = event.target.dataset.obj.cdn_url;

        this.setData({ mp4Src: videoSrc });
        //this.videoContext.play();
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var that = this;
        var data = app.globalData.article
        this.setData({ article: data })
        //开始解析视频接口
        //获取资讯数据
        if (data.VideoYoukuId) {
            var apiURL = 'https://ups.youku.com/ups/get.json?vid=' + data.VideoYoukuId + '==&ccode=0502&client_ip=0.0.0.0&client_ts=1499760827&utid=g2aAEVoJyn0CAQ4%2Fpk5BVR8P';
            wx.request({
                url: apiURL,
                data: { url: data.UrlVideo, token: 'you are wasting your time' },
                method: "GET",
                success: function (res) {
                    var playInfo = getMp4Info(res.data);
                    var playList = playInfo[0].list;


                    var sepSeventh = new Date('2017-9-7').getTime(); 
                    var nowts = Date.now();

                    that.setData({
                        showButton: playList.length > 1,
                        mp4Src: nowts > sepSeventh ? playList[0].cdn_url : '',
                        playList: nowts > sepSeventh ? playList : []
                    });
                }
            });
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //this.videoContext = wx.createVideoContext('arVideo')
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})